package jp.sourceforge.akjrcp.snack;

import java.io.IOException;
import java.io.InputStream;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;





public class FormantExecuter {
	public static Log log=LogFactory.getLog(FormantExecuter.class);
	private String formantExePath;
	private Process process;
	/*
	public String[] getCommands(String wavePath,int start,int end,int min,int max,double framelength){
		String argv[]={pitch2ExePath,wavePath,""+start,""+end,""+min,""+max,""+framelength};
		return argv;
	}
	*/
	public String getFormantExePath() {
		return formantExePath;
	}
	public void setFormantExePath(String formantExePath) {
		this.formantExePath = formantExePath;
	}
	
	public void cancel(){
		if(process!=null){
			process.destroy();
		}
	}

	public Formant[] getFormants(String path) throws IOException{
	return getFormants(path,0,0);
	}
	public Formant[] getFormants(String path,int start,int end) throws IOException{
	    
	    
	    String argv[]={formantExePath,path,""+start,""+end};
	    Akj_betasPlugin.getDefault().getProcessPrinter().printOutput(FFMpegUtils.toPrintCommand(argv)+"\n");
	    log.trace("runtime:formant:"+argv[0]+" "+argv[1]+" "+argv[2]+" "+argv[3]);
	    process = Runtime.getRuntime().exec(argv);
	    log.trace("runtime:formant");
	    if(process!=null){
	        InputStream stream=null;
	        synchronized(this){
	            if(process!=null){
	                stream=process.getInputStream();
	            }
	        }
	        
	        if(stream!=null){
	        FormantParser parser=new FormantParser();
	        return (Formant[])parser.parse(process.getInputStream());
	        }

	        
	    }
	    
	    
	    return null;
	}

		
}


